<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头部图标 -->
      <div class="avatar_box">
        <img src="../assets/logo.svg" alt="" />
      </div>
      <!-- 用户登录表单 -->
      <!-- 这里loginFormRef是引用对象，rules是验证规则 -->
      <el-form
        ref="loginFormRef"
        label-width="0px"
        :model="loginForm"
        :rules="loginFormRules"
        class="login_form"
      >
        <!-- user name -->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username"></el-input>
        </el-form-item>
        <!-- user password -->
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" show-password></el-input>
        </el-form-item>
        <!-- button -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login(loginFormRef)">登录</el-button>
          <el-button type="info" @click="resetLoginForm(loginFormRef)">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script setup lang="ts" name="Login">
import type { FormInstance } from 'element-plus'
import { useUserStore } from '@/stores/user'
import { login as loginApi, getUserInfo } from '@/api'

//接口定义
interface Rule {
  required?: boolean
  message: string
  trigger: string
  min?: number
  max?: number
}
interface RuleForm {
  username: Array<Rule>
  password: Array<Rule>
}
// 仓库取值
const userStore = useUserStore()
console.log({ userStore })
// 自定义类型
const loginFormRef = ref<FormInstance>()
const loginForm = reactive<{ username: string; password: string }>({
  username: 'admin',
  password: '123456'
})
const loginFormRules = reactive<RuleForm>({
  // 验证用户名是否合法
  username: [
    { required: true, message: '请输入用户名称', trigger: 'blur' },
    { min: 2, max: 6, message: '长度在 2 到 6个字符', trigger: 'blur' }
  ],
  // 验证密码是否合法
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      min: 4,
      max: 10,
      message: '长度在 4 到 10 个字符',
      trigger: 'blur'
    }
  ]
})

// 登录
const router = useRouter()
const login = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate(async (valid) => {
    if (!valid) return false
    let ret: any
    console.log('submit!')
    // 调用接口
    ret = await loginApi(loginForm)
    if (ret.code !== 200) {
      return ElMessage({
        message: '登录失败！',
        type: 'warning'
      })
    }
    ElMessage({
      message: '登录成功！',
      type: 'success'
    })
    userStore.SET_TOKEN(ret.data.access_token)
    // 1.将登录成功之后的token，保存到客户端的sessionStorage中
    //  1.1项目中除了登录之外的其他api接口，必须在登录之后才能访问
    //  1.2token只应在当前网站打开期间生效，所以token保存在sessionStorage中
    let res: any
    res = await getUserInfo({})
    // vuex存储用户信息
    if (res.code === 200) {
      userStore.USER_LOGIN(res.data)
      router.replace('/home')
    }
  })
}

// 重置
let resetLoginForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>
<style lang="scss" scoped>
.login_container {
  background: $--color-primary;
  height: 100%;
  .login_box {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 450px;
    height: 300px;
    border-radius: 3px;
    background: $--color-white;
    .avatar_box {
      width: 130px;
      height: 130px;
      padding: 10px;
      border: 1px solid $--color-eee;
      border-radius: 50%;
      box-shadow: 0 0 10px $--color-eee;
      background: $--color-white;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background: $--color-eee;
      }
    }
  }
}
.login_form {
  position: absolute;
  bottom: 0px;
  width: 100%;
  padding: 0 10px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
